/* File:        	Daemon.java
* Package:			org.esi.authz.pdp.core
* Created:      	29-abr-2011
* Last Changed: 	0:08:08
* Author:       	Borja Urkizu borja.urquizu@tecnalia.com
*
*   
* History:
*  Revision 1.0.0.0  
* 
* Tecnalia Research and Innovation
*/
package com.tecnalia.authz.pdp.core;

import com.tecnalia.authz.comm.channel.ReqResDChannelImpl;
import com.tecnalia.authz.common.log.PDPLog;
import com.tecnalia.authz.pdp.config.PDPConfig;
import com.tecnalia.authz.pdp.config.PDPConfiguration;
import com.tecnalia.authz.pdp.core.xacml.IXACMLQuery;
import com.tecnalia.authz.pdp.core.xacml.XACMLQueryImpl;
import com.tecnalia.comm.DaemonImpl;
import com.tecnalia.comm.IDaemon;
import com.tecnalia.comm.channel.IDChannel;



import org.jboss.security.xacml.interfaces.RequestContext;
import org.jboss.security.xacml.interfaces.ResponseContext;


// TODO: Auto-generated Javadoc
/**
 * The Class Daemon.
 */
public class PDPEngine implements IXACMLQuery,IDaemon {
    
    /** The logger. */
    public static PDPLog logger =  new PDPLog (PDPEngine.class); 
    
    /** The PDP engine configuration . */
    private static PDPConfiguration PDPcfg;
   
	/** The object PDP configuration. */
    private PDPConfig pdpconfig;

    /** The daemon Interface. */
    private IDaemon daemon = null;
    
    /** The xacml query Interface. */
    private IXACMLQuery xacmlQuery = null;
    

    
	/**
	 * Instantiates a new daemon.
	 */
	public PDPEngine(){    	
        
    	logger.info("PDP Initilizazed");

    	PDPcfg = new PDPConfiguration();
        
        pdpconfig = PDPcfg.getPdpConfig();        
		
        daemon = new DaemonImpl(pdpconfig.getDaemonHost(), 
        							pdpconfig.getDaemonPort());
        
        daemon.setChannel(new ReqResDChannelImpl());
        
        xacmlQuery = new XACMLQueryImpl(pdpconfig);
        
        //Register the daemon
        PDPInstances.setPdpDaemon(this);
        
        //DaemonStatus.updateProperty(KEY_RUN_STATUS, STATUS_RUN_INITIALIZED);
	}

    /**
     * Gets the pD pcfg.
     *
     * @return the pD pcfg
     */
    public static PDPConfiguration getPDPcfg() {
		return PDPcfg;
	}


    /**
     * Log message.
     *
     * @param message the message
     */
    public void logMessage(String message){
    	logger.info(message);
    }

    /**
     * Checks if is valid id.
     *
     * @param id the id
     * @param name the name
     * @return true, if is valid id
     */
    public static boolean isValidID(int id, String name){
    	//int storedID = (int) pdpconfig.ValidPEP.get(name);
    	
    	if( PDPcfg.getPdpConfig().ValidPEP.containsKey(name) 
    			&& PDPcfg.getPdpConfig().ValidPEP.containsValue(id)){
    		return true;
    	}
    	
    	return false;
    }

    
	/* (non-Javadoc)
	 * @see com.tecnalia.authz.pdp.core.xacml.IXACMLQuery#evaluate(org.jboss.security.xacml.interfaces.RequestContext[], int)
	 */
	@Override
	public ResponseContext[] evaluate(RequestContext[] requestContext, int queryMode) {
		return this.xacmlQuery.evaluate(requestContext, queryMode);
	}

	/* (non-Javadoc)
	 * @see com.tecnalia.authz.pdp.core.xacml.IXACMLQuery#evaluate(org.jboss.security.xacml.interfaces.RequestContext, int)
	 */
	@Override
	public ResponseContext evaluate(RequestContext requestContext, int queryMode) {
		return this.xacmlQuery.evaluate(requestContext, queryMode);
	}

	/* (non-Javadoc)
	 * @see com.tecnalia.comm.IDaemon#start()
	 */
	@Override
	public void start() {
		this.daemon.start();		
	}

	/* (non-Javadoc)
	 * @see com.tecnalia.comm.IDaemon#refresh()
	 */
	@Override
	public void refresh() throws Exception {
		this.daemon.refresh();		
	}

	/* (non-Javadoc)
	 * @see com.tecnalia.comm.IDaemon#cleanup()
	 */
	@Override
	public void cleanup() {
		this.daemon.cleanup();
	}

	/* (non-Javadoc)
	 * @see com.tecnalia.comm.IDaemon#getChannel()
	 */
	@Override
	public IDChannel getChannel() {
		return this.daemon.getChannel();
	}

	/* (non-Javadoc)
	 * @see com.tecnalia.comm.IDaemon#setChannel(com.tecnalia.comm.channel.IDChannel)
	 */
	@Override
	public void setChannel(IDChannel channel) {
		this.daemon.setChannel(channel);		
	}
}
